package jp.mixi.android.sdk.hackathon.entity;

import jp.mixi.android.sdk.hackathon.util.JSONObjectUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PhotoEntity implements Serializable {

    private List<Photo> list;
    private int startIndex;
    private int itemsPerPage;
    private int totalResults;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public PhotoEntity(String src) {
        try {
            JSONObject root = new JSONObject(src);
            JSONArray array = root.getJSONArray("entry");
            list = new ArrayList<Photo>();
            for (int i = 0; i < array.length(); i++) {
                list.add(new Photo(array.getString(i)));
            }
            startIndex = root.getInt("startIndex");
            itemsPerPage = root.getInt("itemsPerPage");
            totalResults = root.getInt("totalResults");

        } catch (JSONException e) {
            throw new IllegalArgumentException("not json", e);
        }
    }

    public List<Photo> getPhotoes() {
        return list;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public int getItemsPerPage() {
        return itemsPerPage;
    }

    public int getTotalResults() {
        return totalResults;
    }

    public class Photo {

        private JSONObject original;

        public Photo(String src) {
            try {
                original = new JSONObject(src);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public List<Photo> getList() {
            return list;
        }

        public String getId() {
            return JSONObjectUtil.getStringByName(original, "id");
        }

        public String getNumComments() {
            return JSONObjectUtil.getStringByName(original, "numComments");
        }

        public String getTitle() {
            return JSONObjectUtil.getStringByName(original, "title");
        }

        public String getViewPageUrl() {
            return JSONObjectUtil.getStringByName(original, "viewPageUrl");
        }

        public String getCreated() {
            return JSONObjectUtil.getStringByName(original, "created");
        }

        public String getOwnerId() {
            return JSONObjectUtil.getStringByName(original, "ownerId");
        }

        public String getDescription() {
            return JSONObjectUtil.getStringByName(original, "description");
        }

        public String getThumbnailUrl() {
            return JSONObjectUtil.getStringByName(original, "thumbnailUrl");
        }

        public String getPrivacy() {
            return JSONObjectUtil.getStringByName(original, "privacy");
        }

        public String getOwner() {
            return JSONObjectUtil.getStringByName(original, "owner");
        }

    }
}
